﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.ComponentModel;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using AntUnion.FrameWork.Model.Models;

namespace RM.Common.DotNetEamil
{
    /// <summary>
    /// 邮件客户端 System.Net
    /// 非 Implicit SSL 邮箱适用 非465端口
    /// </summary>
    public class NetEmailClient
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="Dep_Email">发送人、支持发送多个人每个地址用 ; 号隔开</param>
        /// <param name="Mis_Name">任务名称</param>
        /// <param name="Mis_Describe">内容</param>
        /// <param name="File_Path">附件</param>
        /// <returns></returns>
        public static string MailSending1(string Dep_Email, string Mis_Name, string Mis_Describe, string File_Path)
        {
            string MailUser = "support@vip.17b.com";//System.Configuration.ConfigurationManager.AppSettings["MailUser"].ToString();
            string MailName = "MT5";//System.Configuration.ConfigurationManager.AppSettings["MailName"].ToString();
            string MailHost = "smtpdm-ap-southeast-1.aliyun.com";//System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();
            string MailPwd = "1c3F4Y8j2kDD";// System.Configuration.ConfigurationManager.AppSettings["MailPwd"].ToString();
            MailAddress from = new MailAddress(MailUser, MailName); //邮件的发件人
            MailMessage mail = new MailMessage();

            //设置邮件的标题
            mail.Subject = Mis_Name;//任务名称

            //设置邮件的发件人
            //Pass:如果不想显示自己的邮箱地址，这里可以填符合mail格式的任意名称，真正发mail的用户不在这里设定，这个仅仅只做显示用
            mail.From = from;

            //设置邮件的收件人
            string address = "";
            string displayName = "";
            /**/
            /*  这里这样写是因为可能发给多个联系人，每个地址用 ; 号隔开
                一般从地址簿中直接选择联系人的时候格式都会是 ：用户名1 < mail1 >; 用户名2 < mail 2>; 
                因此就有了下面一段逻辑不太好的代码
                如果永远都只需要发给一个收件人那么就简单了 mail.To.Add("收件人mail");
                */
            string[] mailNames = (Dep_Email + ";").Split(';');
            foreach (string name in mailNames)
            {
                if (name != string.Empty)
                {
                    if (name.IndexOf('<') > 0)
                    {
                        displayName = name.Substring(0, name.IndexOf('<'));
                        address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    else
                    {
                        displayName = string.Empty;
                        address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    mail.To.Add(new MailAddress(address, displayName));
                }
            }

            //设置邮件的抄送收件人
            //这个就简单多了，如果不想快点下岗重要文件还是CC一份给领导比较好
            //mail.CC.Add(new MailAddress("Manage@hotmail.com", "尊敬的领导");

            //设置邮件的内容
            mail.Body = Mis_Describe;
            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            //设置邮件的发送级别
            mail.Priority = MailPriority.Normal;

            //设置邮件的附件，将在客户端选择的附件先上传到服务器保存一个，然后加入到mail中
            if (File_Path != "")
            {
                mail.Attachments.Add(new Attachment(File_Path));
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            }
            SmtpClient client = new SmtpClient();
            //设置用于 SMTP 事务的主机的名称，填IP地址也可以了
            client.Host = MailHost;
            //设置用于 SMTP 事务的端口，默认的是 25
            client.Port = 25;
            client.UseDefaultCredentials = false;
            //这里才是真正的邮箱登陆名和密码， 我的用户名为 MailUser ，我的密码是 MailPwd
            client.Credentials = new System.Net.NetworkCredential(MailUser, MailPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            ////如果发送失败，SMTP 服务器将发送 失败邮件告诉我
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            //都定义完了，正式发送了，很是简单吧！
            client.Send(mail);
            return mail.ToString();
        }


        public static Task SendMailByConfig(string Dep_Email, string Mis_Name, string Mis_Describe, string File_Path)
        {
            using (mtContext context = new mtContext())
            {
                EmailSetting email = context.EmailSettings.Where(k => k.Id == 1).FirstOrDefault();
                if (email == null)
                {
                    return null;
                }
                var message = new MailMessage
                {
                    From = new MailAddress(email.Email, email.From),
                    Subject = Mis_Name,
                    Body = Mis_Describe,
                    BodyEncoding = System.Text.Encoding.UTF8,
                    IsBodyHtml = true
                };
                if (File_Path != "")
                {
                    message.Attachments.Add(new Attachment(File_Path));
                    message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
                }
                //设置邮件的收件人
                string address = "";
                string displayName = "";
                /**/
                /*  这里这样写是因为可能发给多个联系人，每个地址用 ; 号隔开
                    一般从地址簿中直接选择联系人的时候格式都会是 ：用户名1 < mail1 >; 用户名2 < mail 2>; 
                    因此就有了下面一段逻辑不太好的代码
                    如果永远都只需要发给一个收件人那么就简单了 mail.To.Add("收件人mail");
                    */
                string[] mailNames = (Dep_Email + ";").Split(';');
                foreach (string name in mailNames)
                {
                    if (name != string.Empty)
                    {
                        if (name.IndexOf('<') > 0)
                        {
                            displayName = name.Substring(0, name.IndexOf('<'));
                            address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                        }
                        else
                        {
                            displayName = string.Empty;
                            address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                        }
                        message.To.Add(new MailAddress(address, displayName));
                    }
                }
                SmtpClient client = new SmtpClient();
                //设置用于 SMTP 事务的主机的名称，填IP地址也可以了
                client.Host = email.Address;
                client.EnableSsl = (bool)email.EnableSsl;
                //设置用于 SMTP 事务的端口，默认的是 25
                client.Port = (int)email.Port;
                client.UseDefaultCredentials = false;
                //这里才是真正的邮箱登陆名和密码， 我的用户名为 MailUser ，我的密码是 MailPwd
                client.Credentials = new System.Net.NetworkCredential(email.Account, email.Password);
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                ////如果发送失败，SMTP 服务器将发送 失败邮件告诉我
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                return client.SendMailAsync(message);
            }
        }

        /// <summary>
        /// 交易账户开通
        /// </summary>
        /// <param name="crLogin"></param>
        /// <param name="mt5Login"></param>
        /// <param name="mt5password"></param>
        /// <param name="mt5server"></param>
        /// <returns></returns>
        public static string Getsuccessful(string crLogin, string mt5Login, string mt5password, string mt5server)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr> <td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px; \">尊敬的客户</h2></td> </tr>");
            str.AppendFormat(@"<tr><td style='width: 800px; font-size:20px; line-height:45px;padding:10px 45px; color: #000;'>您的MT5交易账户现已开通。<br />
                             CRM账户 : {0}<br />MT5交易账号 :{1}<br />MT5账号密码 : {2}<br /> MT5服务器 : {3}<br /></td></tr>",
                             crLogin, mt5Login, mt5password, mt5server);
            str.Append("<tr><td style=\"width: 800px; font-size: 16px; line-height: 35px; padding:10px 45px;\"><strong style=\"font-weight: bold; font-size: 18px; \">温馨提示:</strong><br />*杠杆交易存在风险。平台的所有操作及交易均为本人进行，本人清楚交易中的盈利或者亏损均自行负责。<br />*为保障阁下的账户安全，我们建议您在收到以上密码后，尽快更改登入密码。有关登入资料也不应该向任何人士透露。祝投资愉快！如您有任何疑问，请您联系专属客服。谢谢！<br /></td></tr>");
            str.Append("</tbody></table> ");
            return str.ToString();

        }

        /// <summary>
        /// 注册邮箱
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetCode(string code, int time)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">账号验证码</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您注册邮箱验证码为：{0}<br />请将此验证码回填至账号界面的邮箱验证码处，继续完成注册。<br /> 请妥善保管，切勿泄露，验证码有效期{1}分钟<br /></td></tr>", code, time);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 资金密码验证码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string GetFundPassword(string code, int time)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">资金密码验证码</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您的账号验证码为：{0}<br />请将此验证码回填至资金密码界面的邮箱验证码处，继续完成注册。<br /> 请妥善保管，切勿泄露，验证码有效期{1}分钟<br /></td></tr>", code, time);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 变更账号验证码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string UpdateEmailCode(string code, int time)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">变更账号验证码</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您的账号验证码为：{0}<br />请将此验证码回填至变更账号界面的邮箱验证码处，继续完成。<br />请妥善保管，切勿泄露，验证码有效期{1}分钟<br /> <br /></td></tr>", code, time);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 修改手机号码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string UpdateTelCode(string code, int time)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">尊敬的客户，您好！</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您的账号正在修改手机号码,验证码为：{0}<br />请妥善保管，切勿泄露，验证码有效期{1}分钟<br /> <br /></td></tr>", code, time);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 变更登录密码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string UpdatePasswordCode(string code, int time)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">变更登录密码</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您的账号正在修改登录密码,验证码为：{0}<br />请妥善保管，切勿泄露，验证码有效期{1}分钟<br /> <br /></td></tr>", code, time);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 忘记密码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ForgetPasswordCode(string code, int time)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">变更账号验证码</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您的账号正在修改登录密码,验证码为：{0}<br />请将此验证码回填至变更账号界面的邮箱验证码处，继续完成。<br />请妥善保管，切勿泄露，验证码有效期{1}分钟<br /> <br /></td></tr>", code, time);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="login">登录账号</param>
        /// <param name="pwd">新密码</param>
        /// <param name="pwdType">密码类型</param>
        /// <returns></returns>
        public static string ResetPassword(long login, string pwd, string pwdType)
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">尊敬的客户，您好！</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">已为您的账号：{0} 重置了{1}密码；<br />新密码为：{2}<br /> <br /></td></tr>", login, pwdType, pwd);
            str.Append("</tbody></table>");
            return str.ToString();
        }
        /// <summary>
        /// 审核失败
        /// </summary>
        /// <param name="login">登录账号</param>
        /// <param name="pwd">新密码</param>
        /// <param name="pwdType">密码类型</param>
        /// <returns></returns>
        public static string AuditFailed()
        {
            StringBuilder str = new StringBuilder();
            str.Append("<table border=\"0\" style=\"width: 800px; margin: 30px auto 0; padding - bottom: 30px; color: #333; border-radius: 6px; box-shadow: 0 0 20px rgba(0,0,0,0.7);\"><tbody>");
            str.Append("<tr><td style=\"width: 800px; text-align: center\"><h2 style=\"height: 48px;\">信息审核失败</h2></td></tr>");
            str.AppendFormat("<tr><td style=\"width: 800px; font-size: 20px; line-height: 45px; padding: 10px 45px; color: #000;\">您提交的个人信息审核失败，请到用户中心查看具体原因，如果疑问请咨询专属客服</td></tr>");
            str.Append("</tbody></table>");
            return str.ToString();
        }
    }

}


